package org.vigor.lab.gateway.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.client.RestTemplate;
import org.vigor.lab.gateway.jwt.JwtHelper;
import org.vigor.lab.gateway.zuul.RedisRouteLocator;
import org.vigor.lab.gateway.zuul.filter.AuthZuulFilter;
import org.vigor.lab.gateway.zuul.filter.LogPostZuulFilter;
import org.vigor.lab.gateway.zuul.filter.LogPreZuulFilter;

/**
 * CustomerZuulConfig
 * <p>
 * </p>
 *
 * @author Vigor Yuan
 */
@Configuration
@EnableConfigurationProperties({ ZuulProperties.class })
public class CustomerZuulConfig {

    @Autowired
    ZuulProperties zuulProperties;

    @Autowired
    ServerProperties server;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    SysConfig sysConfig;

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean
    public RedisRouteLocator routeLocator() {
        RedisRouteLocator routeLocator = new RedisRouteLocator(this.server.getServletPrefix(), this.zuulProperties);
        routeLocator.setRedisTemplate(redisTemplate);
        return routeLocator;
    }

    @Bean
    public AuthZuulFilter authZuulFilter() {
        return new AuthZuulFilter();
    }

    @Bean
    public JwtHelper jwtHelper() {
        return new JwtHelper(sysConfig.getJwtSecret());
    }

    @Bean
    public LogPreZuulFilter logPreZuulFilter() {
        return new LogPreZuulFilter();
    }

    @Bean
    public LogPostZuulFilter logPostZuulFilter(){
        return new LogPostZuulFilter();
    }

}
